package com.lungle.warmdiary.app.view.testcampaign.fragment.status;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.lungle.warmdiary.app.api.remote.ApiFactory;
import com.lungle.warmdiary.app.base.BaseListFragment;
import com.lungle.warmdiary.app.base.ListBaseAdapter;
import com.lungle.warmdiary.app.model.Result;
import com.lungle.warmdiary.app.model.event.CampaignStatueRefreshEvent;
import com.lungle.warmdiary.app.model.response.Campaign;
import com.lungle.warmdiary.app.model.response.CampaignList;
import com.lungle.warmdiary.app.utils.LogUtils;
import com.lungle.warmdiary.app.view.testcampaign.adapter.CampaignAdapter;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.InputStream;
import java.io.Serializable;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;


public abstract class CampaignStatusFragment extends BaseListFragment<Campaign> {

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        isActive = false;
        EventBus.getDefault().register(this);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onResume() {
        isActive = false;
        if (onResumeRefresh) {
            refresh();
            onResumeRefresh = false;
        }
        try {
            mErrorLayout.setNoDataContent("亲，暂无此类型活动，请稍候");
        } catch (Throwable e) {
            LogUtils.printLogD(e.getMessage());
        }
        super.onResume();

        MobclickAgent.onResume(getActivity());


    }

    @Override
    public void onPause() {
        super.onPause();
//        MobclickAgent.onPageEnd(ACTIVE_HOT_SCREEN);
        MobclickAgent.onPause(getActivity());

    }


    @Override
    protected ListBaseAdapter getListAdapter() {
        CampaignAdapter activeAdapter = new CampaignAdapter(getActivity());
        return activeAdapter;
    }


    @Override
    protected CampaignList parseList(InputStream is) throws Exception {
        return CampaignList.parse(is);
    }


    @Override
    protected CampaignList readList(Serializable seri) {
        return (CampaignList) seri;
    }

    @Override
    protected void requestData(boolean refresh) {
        sendRequestData();
    }

    @Override
    protected void sendRequestData() {
        ApiFactory.getUserApi().getCampaignList(getStatus(), mCurrentPage * getPageSize(), getPageSize()).compose(this.<Result<?>>bindToLifecycle())
                .compose(this.<Result<?>>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(mSubscriber);

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }


    abstract int getStatus();

    public void refresh() {
        sendRequestData();

    }

    boolean onResumeRefresh = false;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onStatueChanged(CampaignStatueRefreshEvent event) {
//        mMediaId = event.getId();
//        refresh();
        onResumeRefresh = true;
    }

}
